<?php

require_once 'common.php';

Class Task
{
	public $id = 0;
	public $name ='';
	public $points = 0;
	public $type ='none';
	public $uid = 0;
	public $dueDate;
	public $tag;
	public $url;
	
	public function serialize() {
		return serialize(
				array(
						'id' => $this->id,
						'name' => $this->name,
						'points' => $this->points,
						'type' => $this->type,
						'dueDate' => $this->dueDate,
						'tag' => $this->tag
						
				)
		);
	}
	
	public function unserialize($data) {
		$data = unserialize($data);
	
		$this->id = $data['id'];
		$this->name = $data['name'];
		$this->points =$data['points'];
		$this->type = $data['type'];
		$this->dueDate = $data['dueData'];
		$this->tag = $data['tag'];
	}
		
}

Class MyTaskList
{
	public $list = array();
	
	public function Show()
	{
		return $this->list;
	}
	
	public function Count()
	{
		return count($this->list);
	}
	
	public function Add($task)
	{
		if($task != null)
		{
			array_push($this->list,$task);
		}
	}
	
	public function Delete($id)
	{
		//log_action("Count:".count($this->list));
		for($i=0;$i<count($this->list);$i++)
		{
			if($this->list[$i]->id == $id)
			{
				//log_action("i =" .$i);
				unset($this->list[$i]);// remove $i from list
				$this->list = array_values($this->list);
				//array_slice($this->list,$i,1);
				
				//log_action("Count:".count($this->list));
			}
		}
	}
	
	public function Update($task)
	{
		if($task != null && count($this->list) > 0)
		{
			for($i=0 ; $i<count($this->list) ; $i++)
			{
				if($this->list[$i]->id == $task->id)
				{
					//log_action("the task:".$task->name);
					//log_action("the tasklist[$i]:".$this->list[$i]->name);
					$this->list[$i] = $task;// replcace item
					//log_action("the tasklist[$i]:".$this->list[$i]->name);
				}
			}
		}
	}
	
	public function Taglist()
	{
		$tags = array();
		
		//log_action("count:".count($this->list));
		
		foreach($this->list as $t)
		{
			//log_action("tag:".$t->tag);
			if(strlen($t->tag) > 0 and !in_array($t->tag, $tags))
			{
				array_push($tags, $t->tag);
			}
		}
		
		return $tags;
	}
	
}



